package com.todo.strategy.online.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;

import com.google.common.collect.Lists;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.strategy.common.enums.StrategyRelationTypeEnum;
import com.todo.strategy.online.infrastructure.entity.po.WantRelationPO;
import com.todo.strategy.online.infrastructure.mapper.WantRelationMapper;
import com.todo.strategy.online.infrastructure.repository.WantRelationRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * (WantRelation)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("wantRelationRepository")
public class WantRelationRepositoryImpl implements WantRelationRepository {
    @Resource
    private WantRelationMapper wantRelationMapper;


    @Override
    public Boolean updateWantRelationPOById(WantRelationPO wantRelationPO) {
        if (wantRelationPO == null || wantRelationPO.getId() == null) {
            return false;
        }
        if (!baseFieldCheck(wantRelationPO)) {
            return false;
        }
        return wantRelationMapper.updateWantRelationPOById(wantRelationPO) > 0;
    }

    @Override
    public Boolean removeWantRelationPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return wantRelationMapper.removeWantRelationPOById(id) > 0;
    }

    @Override
    public WantRelationPO queryWantRelationPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return wantRelationMapper.queryWantRelationPOById(id);
    }

    @Override
    public Boolean insertWantRelationPO(WantRelationPO wantRelationPO) {
        if (wantRelationPO == null) {
            return false;
        }
        if (!baseFieldCheck(wantRelationPO)) {
            return false;
        }
        return wantRelationMapper.insertWantRelationPO(wantRelationPO) > 0;
    }

    @Override
    public List<WantRelationPO> queryWantRelationPOByContentId(Long wantId) {
        if (wantId == null || wantId < 0) {
            return Lists.newArrayList();
        }
        return wantRelationMapper.queryWantRelationPOByContentId(wantId);
    }

    @Override
    public Boolean removeWantRelationByIdList(Long wantId, List<Long> idList) {
        if (wantId == null || wantId < 0 || idList == null) {
            return false;
        }
        if (CollectionUtils.isEmpty(idList)){
            return true;
        }
        return wantRelationMapper.removeWantRelationByIdList(wantId,idList);
    }

    private boolean baseFieldCheck(WantRelationPO wantRelationPO) {
        if (wantRelationPO == null) {
            return false;
        }
        if (wantRelationPO.getUserId() == null || wantRelationPO.getWantId() == null ) {
            log.warn("WantRelationRepository#baseFieldCheck 核心字段校验失败，操作失败 wantRelationPO：【{}】", JSONObject.toJSONString(wantRelationPO));
            return false;
        }
        if (!wantRelationPO.getRelationType().equals(StrategyRelationTypeEnum.CUSTOM.getType()) && wantRelationPO.getRelationId() == null){
            log.warn("WantRelationRepository#baseFieldCheck 非自定义数据并且没有关联id，操作失败 wantRelationPO：【{}】", JSONObject.toJSONString(wantRelationPO));
            return false;
        }
        if ( wantRelationPO.getRelationCity() == null || wantRelationPO.getSort() == null) {
            log.warn("WantRelationRepository#baseFieldCheck 基础字段校验失败，操作失败 wantRelationPO：【{}】", JSONObject.toJSONString(wantRelationPO));
            return false;
        }
        return true;
    }
}
